//
//  EncryptResponseAction.m
//  FinancialStore
//
//  Created by zhenqiu on 16/7/19.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "EncryptResponseAction.h"


@implementation EncryptResponseAction

-(instancetype)init{
    
    if(self = [super init]){
        
        // to do
    }
    
    return self;
    
}


+(id)decryptData:(id)data withError:(NSError **)error{
    
    id responseObject = nil;
    
    
    NSInteger code = [[data valueForKey:RS_CODE_KEY] integerValue];
    
    if(code != RS_OK){
        
        NSString * error_msg = [data valueForKey:RS_MSG_KEY];
        NSDictionary * userinfo = @{NSLocalizedDescriptionKey:error_msg};
        *error = [NSError errorWithDomain:HAODAI_FINANCIAL_STORE_DOMAIN code:code userInfo:userinfo];
        
        return responseObject;
    }

    id encryptData = [data valueForKey:RS_DATA_KEY];
    if(encryptData && [encryptData length] > 0)
    {
        
        NSString * auth_key = [self getAuthKey];
        NSString * decryptStr = [AES128Base64Util AES128Decrypt:encryptData withKey:auth_key withIV:(NSString *) AUTH_IV];
        const char* cDecryptStr = [decryptStr UTF8String];
        NSData * data = [NSData dataWithBytes:cDecryptStr length:strlen(cDecryptStr)];
        responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
        
    }
    return responseObject;
}

+(id)decryptStr:(id)str{
    
    NSString *decryptStr = @"";
    if(str && [str length] > 0){
        
        NSString * authKey = [self getAuthKey];
        decryptStr = [AES128Base64Util AES128Decrypt:str withKey:authKey withIV:(NSString *)AUTH_IV];
        if(decryptStr){
            
            const char *cdata = [decryptStr UTF8String];
            NSData * data = [NSData dataWithBytes:cdata length:strlen(cdata)];
            decryptStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            
        }
    }
    
    return decryptStr;
    
}

@end
